
import React from 'react';

import styles from './index.css';

import GiftList from "../../components/Gift";
import SearchPage from "../../components/SearchPage";
import GiftGoodsService from "../../business/gift/GiftGoodsService";
import UserService from "../../business/user/UserService";



class index extends React.Component{
  constructor(props) {
    super(props);
    let username = sessionStorage.getItem('username');
    username = username==undefined?'':username;
    this.state={
      giftDomain:[],
      userPoint:0,
      username:username,
    };
    this.giftGoodsService = new GiftGoodsService();
    this.userService = new UserService();
    this.callBackQueryList = this.callBackQueryList.bind(this);
    this.callBackUserInfo = this.callBackUserInfo.bind(this);
  }
  state={
    giftDomain:[],
    userPoint:0,
    username:"",
  };

  giftGoodsService:GiftGoodsService;
  userService:UserService;

  componentDidMount(){
    console.info("giftPage htpp start");
    this.giftGoodsService.queryList(this.callBackQueryList);
    let username = sessionStorage.getItem('username');
    if(username !=undefined &&username !=''){
      let token = sessionStorage.getItem('loginToken');
      this.userService.queryUserInfo(username,token,this.callBackUserInfo);
    }
  }

  callBackQueryList(gift,isSuccess){
    if(isSuccess){
      this.setState({
        giftDomain: gift.data.dataList
      });
    }
  }

  callBackUserInfo(res,isSuccess){
    if(isSuccess){
      if(res.code = '200'){
        this.setState({
          userPoint: res.data.currentPoints,
          username: sessionStorage.getItem('username'),
        });
      }
    }
  }



  render() {
    const giftName='';
    const {giftDomain,userPoint,username} = this.state;
    return (
      <div className={styles.normal}>
        <SearchPage giftName={giftName} userPoint={userPoint} username={username} />
        {/*<SelectPage/>*/}
        <GiftList gifts={giftDomain} userPoint={userPoint}/>
      </div>
    );
  }
}

export default index;
